<?php
/**
 * Created by PhpStorm.
 * User: yancheng (cheng@love.xiaoyan.me)
 * Date: 15/5/24
 * Time: 下午2:30
 */
class Notification extends BaseController{

    public function init()
    {
        $this->addRoute('/notification.notice','notice');
        $this->addRoute('/notification.read','read');
        $this->addRoute('/notification.list','listNotice');
        $this->addRoute('/notification.view.add','viewAdd');
        $this->addRoute('/notification.add','add');
        $this->addRoute('/notification.delete','delete');
    }
    public function notice(){
        $preTime = $this->getConfig('sys')->getConfig('notice_pre_time')->getValue();
        $prevTime = $this->input()->post('prevRead'); //上一次读取时间
        $where = array(
            'nc.uid' => $this->getUserId(),
            'notice_time[<=]' => REQ_TIME,
            'notice_time[>=]' => $prevTime,
            'notice_state' => DataState::$enable
        );
        $where = array(
            'AND' =>$where
        );
        //查询条数
        $noticeCount = DB()->count('notification_center as nc',$where);
        if($noticeCount > 3){
            ajaxSuccess(array(
                'count' => $noticeCount,
                'd'=>DB()->all_query(),
                'notices' => array(
                    'nid' => 0,
                    'notice_title' => '提醒',
                    'notice_message' => '目前你有'.$noticeCount.'条提醒需要处理,请在
                    <strong style="color:red;">提醒查询</strong> 中查看'
                ),
                'readTime' => REQ_TIME
            ));
        }
        $notice = DB()
            ->field('nc.*,t.task_no')
            ->table('notification_center as nc')
            ->join(array(
                '[><]task#t' => array('nc.task_id'=>'t.task_id')
            ))
            ->where($where)
            ->select();
        foreach($notice as &$n){
            $n['notice_message'] .= '&nbsp;&nbsp;&nbsp;<a href="#" onclick="return osw.noticeOpenTask(\''.$n['task_no'].'\');">☞点击查看详情☜</a>';
        }
        ajaxSuccess(array(
            'count' => $noticeCount,
            'notices' => $notice,
            'readTime' => REQ_TIME
        ));
    }
    public function read(){
        $nid = $this->getInput()->get('nid');
        if($nid > 0){
            $where = array('nid'=>$nid);
//            $notice = DB()->table('notification_center')->where($where)->get();
//            if($notice && $notice['uid'] == $this->getUserId()){
                DB()->update('notification_center',array('notice_state'=>DataState::$invalid),$where);
                ajaxSuccess();
//            }
        }
        ajaxResponse(0,'');
    }
    public function listNotice(){
        list($page,$size,$start) = $this->getPageAndSize(8);
        $where = ' where nc.task_id = t.task_id and nc.uid = 1
and notice_state <>'.NoticeState::$delete;
        $pageString = '';
        $tablePre = $this->getConfig('db_config')->getConfig('db_prefix')->getValue();
        $this->assign('n_state',-1);
        if(($state = $this->getInput()->get('state')) != null && in_array($state,array(1,2,4))){
            $pageString.='state='.$state;$this->assign('n_state',$state);
            if($state == 1){
                $where .=' and notice_time>'.REQ_TIME;
            }
            if($state == 4){
                $state = 1;
                $where .=' and notice_time<='.REQ_TIME;
            }
            $where .=' and notice_state='.$state;
        }
        $this->assign('wd','');
        if(($searchKey = $this->getInput()->get('wd'))){//关键字搜索
            $where .= " and (contact_name like '%{$searchKey}%' or phone like '%{$searchKey}%' "
                ." or task_no like '%{$searchKey}%' or t.task_id like '%{$searchKey}%')";
            $pageString .=''. $searchKey;
            $this->assign('wd',$searchKey);
        }
        $sql = " FROM {$tablePre}notification_center nc,
{$tablePre}task t
{$where}";
        $notice = DB()->query('SELECT * '.$sql." order by nid desc limit {$start},{$size}")->fetchAll();
        $total = DB()->countBySql('SELECT count(1) '.$sql);
        foreach($notice as &$n){//状态
            if($n['notice_time'] < REQ_TIME && $n['notice_state'] == DataState::$enable){
                $n['notice_state'] = DataState::$wait;
            }
        }
        $this->createWindowPageLink('notification.list'.$pageString,$total,$size);
        $this->render('notice.list',array(
            'noticeList' => $notice
        ));
    }
    public function add(){
        $saveData = $this->checkDataNull(array(
            array('notice_message',1,'提醒内容必须填写'),
            array('notice_time',2,'必须设置提醒时间')
        ));
        $saveData['uid'] = $this->getUserId();
        $this->getPostData($saveData,'notice_title');
        $saveData['notice_time'] = strtotime($saveData['notice_time'].':00');
        $nid = $this->ajaxBasicInsert('notification_center',$saveData);
        ajaxSuccess(array(
            'url' => 'notification.list',
            'nid' => $nid
        ));
    }
    public function viewAdd(){
        $this->render('notice.add');
    }
    public function delete(){
        $id = $this->getInput()->post('nid');
        if(!$id || count($id) == 0) ajaxResponse(1,'请选择要删除的提醒');
        try{//尝试更新
            DB()->update('notification_center',array('notice_state'=>DataState::$delete),array('nid'=>$id));
            ajaxSuccess();
        }catch (DBException $e){
            ajaxResponse(2,'删除提醒失败',$e->getMessage());
        }
    }


}